﻿using Autofac;
using ggHD.DAL.Modules;
using ggHD.Domain.BizObj;
using ggHD.Domain.Repisitories;
using NLog;
using OpenPop.Mime;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ggHD.Data
{
    public class TicketService : LoggedService, ITicketService
    {
        private static IContainer _IoC;
        private Lazy<ITicketRepository> _ticketRep = new Lazy<ITicketRepository>(() => _IoC.Resolve<ITicketRepository>());
        private ITicketRepository TicketRepository
        {
            get
            {
                return _ticketRep.Value;
            }
        }

        public TicketService()
        {
            ContainerBuilder b = new ContainerBuilder();
            b.RegisterModule(new DAL.Modules.DalModule());
            _IoC = b.Build();
        }

        public int CreateTicketFromRawMail(long rawMail)
        {
            try
            {
                if (rawMail < 1)
                    throw new TicketServiceException("Неверный идентификатор сообщения");

                return TicketRepository.CreateFromRawMail(mailId: rawMail);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("CreateTicketFromRawMail", ex);
                return 0;
            }
        }

        public IEnumerable<TicketHeader> SelectHeaders(DateTime dateFrom, DateTime dateTo)
        {
            var res = TicketRepository.AllHeaders();
            var intr = res.ToArray()[3].Initiator ?? null;
            return res;
        }
        
       
    }
}
